package load

import (
	"bytes"
	"fmt"
	"github.com/danicat/simpleansi"
	"strconv"
	"util"
)

/**
 * @Author：落叶知秋
 * @Date：16:11 2022/6/16
 * @note：
 **/

func (this *Loadmap) PrintScreen() {
	//使用这个函数，确保刷新的每一帧都是一个空白帧·
	simpleansi.ClearScreen()
	for _, line := range Maze {
		for _, char := range line {
			switch char {
			case '#':
				fmt.Print(simpleansi.WithBlueBackground(util.Cfg.Wall))
			case '.':
				fmt.Print(util.Cfg.Dot)
			case 'X':
				fmt.Print(util.Cfg.Pill)
			default:
				fmt.Print(util.Cfg.Space)
			}
		}
		fmt.Println()

	}
	moveCursor(this.player.Row, this.player.Col)
	fmt.Print(util.Cfg.Player) //写人物
	for _, ghost := range this.ghosts {
		moveCursor(ghost.Position.Row, ghost.Position.Col)
		//写鬼
		if ghost.Status == util.GhostStatusNormal {
			fmt.Print(util.Cfg.Ghost)
		} else {
			fmt.Print(util.Cfg.GhostBlue)
		}
		//fmt.Print(ghost.status)
	}
	// Move cursor outside of maze drawing area
	simpleansi.MoveCursor(len(Maze)+1, 0)

	liveRemaining := strconv.Itoa(this.Lives) //转成字符串
	if util.Cfg.UseEmoji {
		liveRemaining = getLiveAEmoji(this.Lives)
	}
	fmt.Println("Score:", this.Score, "\tlives:", liveRemaining)
	//fmt.Println(this.Lives)
	//fmt.Println("Score:",this.Score,"\tlives:",this.Lives,"\t",liveRemaining) //计分面板
}
func getLiveAEmoji(lives int) string {
	buf := bytes.Buffer{} //创建一个实现了读写方法的字节缓冲
	for i := lives; i > 0; i-- {
		buf.WriteString(util.Cfg.Player)
	}
	return buf.String()
}
func moveCursor(row, col int) {
	//是否使用笑脸
	if util.Cfg.UseEmoji {
		//为什么要*2
		simpleansi.MoveCursor(row, col*2)
	} else {
		simpleansi.MoveCursor(row, col)
	}
}
